#!c:/Perl/bin/perl
# (c) 2009, nizamov@inbox.ru

	use LWP 5.64;
	use HTTP::Cookies; 
	use HTTP::Request::Common;
	use URI::Escape;
	
#	my $use_proxy = 'http://127.0.0.1:8118/'; # proxy address here
	
	my $login_mail = '';
	my $login_pass = '';
	my $userid = ''; # YOUR User ID here!!
	
	# leave this zero if you want to create new albums (as much as @img_dirs)
	# set this -1 if you want to create new albums (as much as all images)
	# set album id if you want to post in an existing one
	my $album_id = 0;
	my $groupid = '0'; # vitcim's GID
	my $oid = ($groupid ? $groupid : $userid);

	# ONE dir per album. More dirs, more new albums
	my @img_dirs = ("./img","./img2"); 
	
	# new albums names (will use random)
	my @album_names = ('NIGRAS1','NIGRAS2','NIGRAS3','NIGRAS4','NIGRAS5','NIGRAS6');
	
	my $useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1;) Firefox/2.0.0.0';
	my $url_login = 'http://vkontakte.ru/login.php';
	my $url_photos = 'http://vkontakte.ru/photos.php';
	my $url_upload = 'http://cs218.vkontakte.ru/upload.php';
	
	my $ua = LWP::UserAgent->new(keep_alive => 1, requests_redirectable => [ 'POST', 'GET' ]);
	$ua->proxy('http', $use_proxy) if $use_proxy;
	$ua->agent($useragent);
	$ua->cookie_jar({});
	
	print 'Login... ';
	my $response = $ua->post($url_login, 
		[	'email' => $login_mail, 
			'pass' => $login_pass, 
		]);
	die 'Login error -- ', $response->status_line
		unless $response->is_success or $response->code == 302;
	print "Success.\n";
sleep 2;
#	print 'Get URL content... ';
#	$url_new = 'http://vkontakte.ru/photos.php?' . "act=new";
#	$response = $ua->get($url_new);
#	die 'Get page error -- ', $response->status_line
#		unless $response->is_success or $response->code == 302;
#	print "Success.\n";
#	my $cont = $response->content;
#	open (LOG, ">log.html");
#	print LOG $cont;
#	close (LOG);

	srand;

sleep 2;

L0:	foreach $img_dir (@img_dirs) {
		# read dir with content
		opendir(DIR,$img_dir) or do { warn "opendir error!"; next L0; };
		@images= readdir(DIR);
		closedir(DIR);
		# start process if we want to post in the existing album
		my $aid = $album_id;
		goto L1 if $album_id;
		# create new album
		print 'Get URL content... ';
		$url_new = 'http://vkontakte.ru/photos.php?' . "act=new";
		$response = $ua->get($url_new);
		die 'Get page error -- ', $response->status_line
			unless $response->is_success or $response->code == 302;
		print "Success.\n";
		my $cont = $response->content;

		$_ = $cont;
		my ($hash) = /name\s*=\s*\"hash\"\s*value\s*=\s*\"(\w+)\"/;
		my $album_name = $album_names[int(rand($#album_names + 1))];
		$aid = new_album($ua,$url_photos,$album_name,$album_name,$groupid,$hash);

	L1:	foreach $img_name (@images) {
			next if(($img_name eq ".") || ($img_name eq ".."));
			if ($album_id < 0) {
#				print "Create album ($img_name)... ";
				$aid = new_album($ua,$url_photos,$img_name,$img_name,$groupid,$hash);
				next if ($aid<0);
			}
			print "Image: $img_dir/$img_name ";
			print "Upload... ";
			# get server URL
#			http://vkontakte.ru/photos.php?act=add&id=92109929&oid=25885430
			$response = $ua->get("$url_photos\?act=add\&id=$aid\&oid=$oid");
			do { warn "Get images server URL error -- ", $response->status_line; next L1; }
				unless $response->is_success or $response->code == 302;
			$_ = $response->content =~ m|name\s*=\s*\"upload\"\s*action\s*=\s*\"([^\"]+)\"|ig;
			$url_upload = $1;
			
			# get hash values
			$_ = $response->content;
			my ($hash) = /name=\"hash\" value=\"(\w+)\" /;
			my ($rhash) = /name=\"rhash\" value=\"(\w+)\" /;
			
			# send image
			my $req = POST $url_upload, Content_Type => 'multipart/form-data',
				Content      => [
					act => 'do_add',
					id => $aid,
					aid => $aid,
					gid => $groupid,
					mid => $userid,
					hash => $hash,
					rhash => $rhash,
					file1 =>  [ "$img_dir/$img_name" ]
				];
			$response = $ua->request($req);
			do { warn "Upload error -- ", $response->status_line; next L1; }
				unless $response->is_success or $response->code == 302;	
#			print "Success... ";
			$_ = $response->content;
			
			# now set newly loaded image as a cover of the album
			print "Set as a cover... ";
			$response = $ua->get("$url_photos\?act=editphotos\&id=$aid\&oid=$oid\&u=1");
			$_ = $response->content;
			(@_) = /setCover\(this, '(\d+_\d+)'/gm;
			my $fid = $_[$#_];
			$response = $ua->post("$url_photos\?act=editphotos\&id=$aid\&oid=$oid\&u=1", 
				[	'act' => 'a_cover',
					'fid' =>  $fid,
					'aid' => $aid, 
				]);
			do { warn " Cover setting error -- ", $response->status_line; next L1; }
				unless $response->is_success or $response->code == 302;
			print "Success.\n";
			sleep 1;
		}
	}
	
	sub new_album {	
		# create new album
		my ($agent,$url,$album_name,$album_desc,$gid,$hash) = @_;
		
		print "Create album ($album_name)... ";
#gid, access_level - for group albom
		my $req = POST $url, Content_Type => 'multipart/form-data',
			Content      => [
				'act' => 'create',
				'hash' => $hash,
				'gid' => $gid ,
				'name' => $album_name,
				'desc' => $album_desc,
				'visible' => '1',
				'access_level' => '0',
			];
		$response = $ua->request($req);
		do { warn "Create new album error -- ", $response->status_line; return -1; }
			unless $response->is_success or $response->code == 302;
		print "Success.\n";
		$_ = $response->base;
($rid) = /id=(\d+)/;
return $rid;
}